package com.fjh.controller;

import com.fjh.pojo.Teacher;
import com.fjh.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping
    public ResponseEntity<Teacher> addTeacher(@RequestBody Teacher teacher) {
        try {
            Teacher savedTeacher = teacherService.addTeacher(teacher);
            return ResponseEntity.status(HttpStatus.CREATED).body(savedTeacher);
        } catch (Exception e) {
            e.printStackTrace();  // 这里可以改成将异常信息写入日志
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping
    public List<Teacher> getAllTeachers() {
        return teacherService.findAllTeachers();
    }

    @GetMapping("/{teacherId}")
    public ResponseEntity<Teacher> getTeacherById(@PathVariable Long teacherId) {
        Optional<Teacher> optionalTeacher = teacherService.findTeacherById(teacherId);
        return optionalTeacher.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @GetMapping("/subject/{subject}")
    public List<Teacher> getTeachersBySubject(@PathVariable String subject) {
        return teacherService.getTeachersBySubject(subject);
    }

    @GetMapping("/school/{school}")
    public List<Teacher> getTeachersBySchool(@PathVariable String school) {
        return teacherService.getTeachersBySchool(school);
    }

    @GetMapping("/login/{loginId}")
    public ResponseEntity<Teacher> getTeacherByLoginId(@PathVariable Long loginId) {
        Teacher teacher = teacherService.getTeacherByLoginId(loginId);
        return teacher != null ? ResponseEntity.ok(teacher) : ResponseEntity.notFound().build();
    }

    @GetMapping("/search/{teacherName}")
    public List<Teacher> searchTeachersByName(@PathVariable String teacherName) {
        return teacherService.searchTeachersByName(teacherName);
    }

    @PutMapping("/{teacherId}")
    public ResponseEntity<Teacher> updateTeacher(@PathVariable Long teacherId, @RequestBody Teacher teacher) {
        Optional<Teacher> optionalTeacher = teacherService.findTeacherById(teacherId);

        if (optionalTeacher.isPresent()) {
            Teacher existingTeacher = optionalTeacher.get();
            // Update the existing teacher with the new data
            existingTeacher.setTeacherName(teacher.getTeacherName());
            existingTeacher.setSubject(teacher.getSubject());
            existingTeacher.setProfession(teacher.getProfession());
            existingTeacher.setSchool(teacher.getSchool());
            existingTeacher.setIntroduce(teacher.getIntroduce());
            // Update any other fields as needed

            Teacher updatedTeacher = teacherService.updateTeacher(existingTeacher);
            return ResponseEntity.ok(updatedTeacher);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{teacherId}")
    public ResponseEntity<Void> deleteTeacher(@PathVariable Long teacherId) {
        Optional<Teacher> optionalTeacher = teacherService.findTeacherById(teacherId);

        if (optionalTeacher.isPresent()) {
            teacherService.deleteTeacher(teacherId);
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}


